package com.sora.user.controller;

import com.sora.basic.exception.BusinessException;
import com.sora.basic.util.AjaxResult;
import com.sora.user.dto.LoginDto;
import com.sora.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 后台系统的登录接口
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = logininfoService.loginAccount(loginDto);
            return new AjaxResult(true,map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }


    /**
     * 后台系统的微信扫码登录接口
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult loginWechat (@RequestBody Map<String,String> map){
        try {
            return logininfoService.loginWechat(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }


    /**
     * 微信三方登录绑定手机号码的接口
     * @param map
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult loginWechatBinder (@RequestBody Map<String,String> map){
        try {
            return logininfoService.loginWechatBinder(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙，请稍后再试！");
        }
    }
}
